/**
 * OrderProcessJmsImpl.java 13.08.2010
 */
package org.example.booking;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.jws.WebMethod;

/**
 * <p>
 * </p>
 * @author tserge
 * @since 13.08.2010
 */
public class OrderProcessBase
{
  private final AtomicInteger nextOrderId = new AtomicInteger( 0 );
  private final OrderDAO dao = new OrderDAO();

  @WebMethod
  public Order createOrder( final Order order )
  {
    if ( order.getOrderId() == null ) {
      order.setOrderId( String.valueOf( nextOrderId.incrementAndGet() ) );
      dao.persist( order );
    }

    order.setStatus( OrderStatus.CREATED );
    return order;
  }


  @WebMethod
  public Order getOrder( final String orderId )
  {
    return dao.get( orderId );
  }

  @WebMethod
  public Order updateOrder( final Order order )
  {
    dao.persist( order );
    return order;
  }


  public class OrderDAO
  {
    private final ConcurrentHashMap<String, Order> storage = new ConcurrentHashMap<String, Order>();

    public void persist( final Order order )
    {
      storage.put( order.getOrderId(), order );
    }

    public Order get( final String orderId )
    {
      return storage.get( orderId );
    }

  }
}